<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace support\command\workerman;

use support\command\workerman\Worker as W;
use Dotenv\Dotenv;
use support\bootstrap\Container;
use Webman\Config;
use Workerman\Worker;

class MuiltWorker implements W
{
    public function register()
    {
        foreach (config('process', []) as $process_name => $config) {
            $worker = new Worker($config['listen'] ?? null, $config['context'] ?? []);
            $property_map = [
                'count',
                'user',
                'group',
                'reloadable',
                'reusePort',
                'transport',
                'protocol',
            ];
            $worker->name = $process_name;
            foreach ($property_map as $property) {
                if (isset($config[$property])) {
                    $worker->$property = $config[$property];
                }
            }

            $worker->onWorkerStart = function ($worker) use ($config) {
                foreach (config('autoload.files', []) as $file) {
                    include_once $file;
                }
                Dotenv::createMutable(base_path())->load();
                Config::reload(config_path(), ['route']);

                $bootstrap = $config['bootstrap'] ?? config('bootstrap', []);
                if (!in_array(\support\bootstrap\Log::class, $bootstrap)) {
                    $bootstrap[] = \support\bootstrap\Log::class;
                }
                foreach ($bootstrap as $class_name) {
                    /** @var \Webman\Bootstrap $class_name */
                    $class_name::start($worker);
                }

                foreach ($config['services'] ?? [] as $server) {
                    if (!class_exists($server['handler'])) {
                        echo "process error: class {$config['handler']} not exists\r\n";
                        continue;
                    }
                    $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
                    if (isset($server['listen'])) {
                        echo "listen: {$server['listen']}\n";
                    }
                    $class = Container::make($server['handler'], $server['constructor'] ?? []);
                    worker_bind($listen, $class);
                    $listen->listen();
                }

                if (isset($config['handler'])) {
                    if (!class_exists($config['handler'])) {
                        echo "process error: class {$config['handler']} not exists\r\n";
                        return;
                    }

                    $class = Container::make($config['handler'], $config['constructor'] ?? []);
                    worker_bind($worker, $class);
                }

            };
        }
    }

}
